<?php
namespace Home\Controller;
class UserController extends CommonController{
	// 过滤不需要登录动作
	public $filter_action = array(
		'register', 
		'forget_password', 
		'valid_field', 
		'get_smscode', 
		'lawyer_register'
	);

	public function _initialize(){
		parent::_initialize();
		if(!in_array(ACTION_NAME, $this->filter_action)){
			$this->user_id();
		}
	}
	//用户中心
	public function index(){
		$this->add_step('用户中心');
		

		// 未完成订单
		$where['user_id'] = $this->user_id;
		$where['status']  = array('not in', array('cancel', 'finish'));
		$unfinish_list = D('Order')->get_list($where);

		// 已完成订单
		$where['user_id'] = $this->user_id;
		$where['status']  = 'finish';
		$finish_list = D('Order')->get_list($where);

		$this->assign('finish_list', $finish_list);
		$this->assign('unfinish_list', $unfinish_list);
		$this->assign('doing_num', D('Order')->get_user_doing_num($this->user_id));
		$this->assign('finish_num', D('Order')->get_user_finish_num($this->user_id));
		$this->display();
	}
	//个人信息
	public function basic_info(){
		$this->add_step('个人信息');
		if(IS_POST){
			$realname = $this->_post('realname');
			$nickname = $this->_post('nickname');
			$sex      = $this->_post('sex');
			$city_id  = $this->_post('city_id');
			$area_id  = $this->_post('area_id');
			if(D('User')->edit_info($this->user_id, $realname, $nickname, $sex, $city_id, $area_id)){
				$this->success('修改成功！', URL('User/basic_info'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}
		$this->assign('info', $this->user);
		$this->display();
	}
	//更换手机号
	public function change_mobile(){
		$this->add_step($this->user_mobile ? '更换手机号' : '绑定手机号');
		if(!$this->set_password){
			$this->error('请先设置密码！');
		}
		if(IS_POST){
			$mobile   = $this->set_password ? $this->_post('mobile', '手机号不能为空！') : '';
			$smscode  = $this->_post('smscode', '短信验证码不能为空！');
			$password = $this->set_password ? $this->_post('password', '密码不能为空！') : '';
			$password = md5($password);
			if(!D('User')->check_password($this->user_id, $password)){
				$this->error(D('User')->tips_info);
			}

			if(D('User')->change_mobile($this->user_id, $mobile)){
				//更新一下用户信息
				D('User')->get_user_info();
				$this->success('修改成功！', URL('User/index'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}

		$this->display();
	}
	//更换E-mail
	public function change_email(){
		$this->add_step('更换E-mail');
		if(!$this->set_password){
			$this->error('请先设置密码！');
		}
		if(IS_POST){
			$email    = $this->_post('email', 'E-mail不能为空！');
			$password = $this->_post('password', '密码不能为空！');
			$password = md5($password);
			if(!D('User')->check_password($this->user_id, $password)){
				$this->error(D('User')->tips_info);
			}

			if(D('User')->change_email($this->user_id, $email)){
				//更新一下用户信息
				D('User')->get_user_info();
				$this->success('修改成功！',  URL('User/index'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}

		$this->display();
	}

	//修改头像
	public function upload_head(){
		$this->add_step('修改头像');
		if(IS_POST){
			$file_id = $this->_post('file_id', '请上传照片');
			$x1      = $this->_post('x1', '', 0);
			$y1      = $this->_post('y1', '', 0);
			$x2      = $this->_post('x2', '请上传照片');
			$y2      = $this->_post('y2', '请上传照片');
			$width   = $this->_post('width', '请上传照片');
			$height  = $this->_post('height', '请上传照片');
			if(D('User')->user_head_crop($this->user_id, $file_id, $x1, $y1, $x2, $y2, $width, $height)){
				$this->success('保存成功！', URL('User/upload_head', '', 'user_upload_head'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}
		$this->display();
	}
	//修改密码
	public function change_password(){
		$this->add_step('修改密码');
		if(IS_POST){
			$old_password = $this->set_password ? $this->_post('old_password', '旧密码不能为空！') : '';
			$new_password = $this->_post('password', '新密码不能为空！');

			$old_password = md5($old_password);
			$new_password = md5($new_password);
			if(D('User')->change_password($this->user_id, $old_password, $new_password)){
				$this->success('修改成功！', U('User/index'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}
		$this->display();
	}
	// 专属客服
	public function customer_service(){
		$this->add_step('专属客服');

		$info = D('User')->get_admin_info($this->user_service_id);
		$this->assign('info', $info);
		$this->display();
	}
	public function auth_email(){
		if(!$this->user_email){
			$this->error('请先设置E-mail！');
		}
		$data['user_id']    = $this->user_id;
		$data['email']      = $this->user_email;
		$data['token_time'] = time();
		$json = json_encode($data);

		$email_title = '米律邮箱账号认证';
		$auth_link   = U('User/confirm_email', array('token' => encode($json)));
		$content     = '<a href="' . $auth_link . '">验证邮箱' . $auth_link .'</a>';
		// 发送邮件
		if(D('Mail')->send($this->user_email, '', $email_title, $content, true, $filename)){
			$this->success('发送成功，请注意查收！');
		}else{
			$this->error('发送失败->' . D('Mail')->tips_info);
		}
	}
	// 验证确认邮箱
	public function confirm_email(){
		$token = $this->_get('token');
		$data  = json_decode(decode($token), true);
		if(!$data['user_id'] || !$data['email'] || !$data['token_time']){
			$this->error('对不起，您的操作有误！');
		}
		if($this->user_id == $data['user_id'] && $this->user_email == $data['email']){
			$where['user_id'] = $this->user_id;
			D('User')->where($where)->save(array('is_auth_email' => 1));
			$this->success('邮箱验证成功！', U('User/basic_info'));
		}
		$this->error('验证失败！');
	}

	//找回密码
	public function forget_password(){
		$this->add_step('找回密码');
		if(IS_POST){
			$mobile     = $this->_post('mobile', '请输入手机号！');
			$smscode    = $this->_post('smscode', '验证码不能为空！');
			$password   = $this->_post('password', '请输入密码！');
			$repassword = $this->_post('repassword', '请再次输入密码！');
			if($password != $repassword){
			    $this->success('两次密码不一致！');
			}
			if(D('Sms')->check_smscode($mobile, $smscode)){
				 $this->error('短信验证码不正确！');
			}

			$password = md5($password);
			if(D('User')->forget_password($mobile, $password)){
				$this->success('找回密码成功！', URL('Login/index', '', 'login'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}
		$this->display();
	}
	//绑定设置
	public function sso_bind(){
		$this->add_step('绑定设置');
		
		$this->display();
	}
	//微博登录回调操作
	public function weibo_bind(){
		$weibo_user = session('weibo_user_info');
		if(!$weibo_user){
			session('weibo_callback_url', U('User/weibo_bind'));
			redirect(U('Login/weibo'));
		}
		$open_id  = $weibo_user['uid'];
		$nickname = $weibo_user['screen_name'];
		//微博绑定成功操作
		if($this->api->sso_bind('weibo', $open_id, $nickname)){
			//清空微博登录数据
			session('weibo_token_info', null);
			session('weibo_user_info', null);
			session('weibo_callback_url', null);
			$this->success('绑定成功！', URL('User/sso_bind', '', 'user_sso_bind'));
		}else{
			$this->error('绑定失败！');
		}
	}
	//微信绑定
	public function weixin_bind_callback(){
		$weixin_code = $this->_get('code');
		if($weixin_code){
			import("WeixinWebOAuth", 'Public/Class/Weixin');
			$weixin = new \WeixinWebOAuth();
			$token_openid = $weixin->get_token_openid($weixin_code);
			if(!$token_openid['token']){
				$this->error('微信登录超时，请重新登录！');
			}

			$weixin_user = $weixin->get_user_info($token_openid['token'],$token_openid['openid']);
			if(!$weixin_user['unionid']){
				$this->error('微信登录超时，请重新登录！');
			}

			$open_id = $weixin_user['unionid'];
			//微博绑定成功操作
			if($this->api->sso_bind('weixin', $open_id, $nickname)){
				$this->success('绑定成功！', URL('User/sso_bind', '', 'user_sso_bind'));
			}else{
				$this->error('绑定失败！');
			}
		}
	}
	/**
	 * 取消绑定
	 */
	public function unbind(){
		$type = $this->_get('type', '请选择要取消绑定类型！');
		if($this->api->sso_unbind($type)){
			$this->success('取消绑定成功！', URL('User/sso_bind', '', 'user_sso_bind'));
		}else{
			$this->error($this->api->api_reinfo);
		}
	}
	
	//用户注册
	public function register(){
		$this->add_step('用户注册');
		if(IS_POST){
			$mobile     = $this->_post('mobile', '请输入手机号！');
			$smscode    = $this->_post('smscode', '短信验证码不能为空！');
			$password   = $this->_post('password', '请输入密码！');
			$repassword = $this->_post('repassword', '请再次输入密码！');
			$email      = $this->_post('email', 'E-mail不能为空！');
			if($password != $repassword){
			    $this->success('两次密码不一致！');
			}
			if(D('Sms')->check_smscode($mobile, $smscode)){
				 $this->error('短信验证码不正确！');
			}
			$password_md5 = md5($password);
			$user_info = D('User')->register($mobile, $email, $password_md5);
			if($user_info){
				D('User')->save_session($user_info);
				$jumpurl = session('login_jumpurl');
				$jumpurl = $jumpurl ? $jumpurl : U('Index/index');
				$this->success('注册成功！', $jumpurl);
			}else{
				$this->error(D('User')->tips_info);
			}
		}
		$this->display();
	}
	//律师注册
	public function lawyer_register(){
		$this->add_step('律师注册');
		if(IS_POST){
			$mobile     = $this->_post('mobile', '请输入手机号！');
			$smscode    = $this->_post('smscode', '短信验证码不能为空！');
			$password   = $this->_post('password', '请输入密码！');
			$repassword = $this->_post('repassword', '请再次输入密码！');
			$email      = $this->_post('email', 'E-mail不能为空！');
			if($password != $repassword){
			    $this->success('两次密码不一致！');
			}
			if(D('Sms')->check_smscode($mobile, $smscode)){
				 $this->error('短信验证码不正确！');
			}
			$password_md5 = md5($password);
			$user_info = D('User')->register($mobile, $email, $password_md5, 1);
			if($user_info){
				D('User')->save_session($user_info);
				$this->success('注册成功！', U('LawyerCenter/perfect_data'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}
		$this->display();
	}
	//退出登录
	public function logout(){
		session('user_token', null);
		redirect(URL('Login/index', '', 'login'));
	}

}